package top.soulblack.purchasesale.controller.setting;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.GoodsSize;
import top.soulblack.purchasesale.model.GoodsStyle;
import top.soulblack.purchasesale.service.GoodsStyleService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * create by 小川
 */

@Controller
@RequestMapping("/style")
public class GoodsStyleController {

    @Autowired
    private GoodsStyleService goodsStyleService;

    @ResponseBody
    @RequestMapping("/addStyle")
    public Result<Boolean> addStyle(GoodsStyle goodsStyle) {
        // 判断是否存在当前款式
        if (goodsStyleService.existStyle(goodsStyle.getName())) {
            return Result.error(STYLE_EXIST);
        }
        goodsStyleService.addStyle(goodsStyle);
        return Result.success(true);
    }

    @ResponseBody
    @RequestMapping("/editStyle")
    public Result<Boolean> editStyle(GoodsStyle goodsStyle) {
        System.out.println(goodsStyle.getId());
        if (goodsStyleService.updateStyle(goodsStyle)) {
            return Result.success(true);
        }
        return Result.error(STYLE_EDIT_ERROR);
    }

    @ResponseBody
    @RequestMapping("/deleteStyle")
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteStyle(Integer styleId) {
        System.out.println(styleId);
        if (goodsStyleService.selectStyle(styleId)){
            return Result.error(STYLE_LIFE);
        }
        if (goodsStyleService.deleteStyle(styleId)) {
            return Result.success(true);
        }
        return Result.error(STYLE_DELETE);
    }
}
